ReactJS Ultra For Beginners
🎯 The useState Hook
What is useState?
- A React Hook that lets you add state to functional components
- Returns an array with the current state value and a function to update it
- Triggers re-renders when state changes
📝 Basic Syntax
import React, { useState } from "react"; function Counter() { const [count, setCount] = useState(0); return ( <div> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); }
🔑 Key Concepts
-
Initial Value
const [count, setCount] = useState(0); // 0 is the initial value -
State Update Function
setCount(count + 1); // Direct value setCount((prevCount) => prevCount + 1); // Function form (safer) -
Multiple State Variables
const [name, setName] = useState(""); const [age, setAge] = useState(0); const [isActive, setIsActive] = useState(false);
⚠️ Important Rules
- Never modify state directly:
count = count + 1❌ - Always use the setter function:
setCount(count + 1)✅ - State updates are asynchronous
- State is preserved between re-renders
🎯 Common Patterns
Toggle State:
const [isVisible, setIsVisible] = useState(false); const toggleVisibility = () => { setIsVisible(!isVisible); };
Form Input:
const [inputValue, setInputValue] = useState(""); const handleChange = (e) => { setInputValue(e.target.value); };
Object State:
const [user, setUser] = useState({ name: "", email: "", age: 0, }); const updateUser = (field, value) => { setUser((prevUser) => ({ ...prevUser, [field]: value, })); };
111 / 111 videos100% complete
Course Completed!
Great job! You've finished all videos.
Welcome to the Course
Intro to The Course
3:33How to Learn This Course
3:02What is React and Why You Should Learn It?
4:14Getting Started with React
Installing Nodejs
3:03Setting up a React Development Environment
4:30VSCode Extensions
3:17JavaScript Refresher - All JS You Need To Know Before React
Functions
5:39Ternary and Logical Operators
7:21Objects and Arrays
7:11Map and Filter Functions
7:15Array Function Key Prop
3:13String Templating
4:27Basic React Concepts
React Project Folder Structure
6:55An Intro To JSX
10:33What is the App Component?
2:46How to use Components?
10:23Lists in React
9:45CSS in React
9:10Component Project - Build A Simple Website
First React Project
3:34Making A Navbar
7:11Displaying Images
7:11Making the About Us Section
2:35Creating A Footer
3:38Intro To States In React
What is the useState Hook?
10:38What is the useState Hook? Part 2
4:42What is the useState Hook? Part 3
14:50